package com.madi.company.app;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.bugtags.library.Bugtags;
import com.jaydenxiao.common.baseapp.BaseApplication;
import com.jaydenxiao.common.commonutils.LogUtils;
import com.jaydenxiao.common.commonutils.SPUtils;
import com.madi.company.bean.UserInfoEntity;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.io.UnsupportedEncodingException;

/**
 * Created by 丁宁
 * on 2016/12/8.
 */

public class GlobalApplication extends BaseApplication {
    private static GlobalApplication mContext;
    private UserInfoEntity.UserBean userModel;

    public static GlobalApplication getInstance() {
        return mContext;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //初始化logger
        LogUtils.logInit(true);
        mContext = this;
        //在这里初始化
        Bugtags.start("80314cb0dcbdc58b6a86aad78b5af13b", this, Bugtags.BTGInvocationEventBubble);
    }

    /**
     * 设置accesstoken
     *
     * @param value
     */
    public static void setAccessToken(String value) {
        SPUtils.setSharedStringData(mContext, "AccessToken", value);
    }

    /**
     * 获取accesstoken
     *
     * @author 丁宁
     * @description
     */
    public static String getAccessToken() {
        return SPUtils.getSharedStringData(mContext, "AccessToken");
    }

    /**
     * 设置RefreshToken
     *
     * @param value
     */
    public static void setRefreshToken(String value) {
        SPUtils.setSharedStringData(mContext, "RefreshToken", value);
    }

    /**
     * 获取RefreshToken
     * @author 丁宁
     * @description
     */
    public static String getRefreshToken() {
        return SPUtils.getSharedStringData(mContext, "RefreshToken");
    }

    /**
     * 存储对象
     * @param model
     */
    public void setUserModel(UserInfoEntity.UserBean model) {
        if (model != null) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream;
            try {
                objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
                objectOutputStream.writeObject(model);
                String serStr = byteArrayOutputStream.toString("ISO-8859-1");
                serStr = java.net.URLEncoder.encode(serStr, "UTF-8");
                SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
                SharedPreferences.Editor editor = preferences.edit();
                if (editor.putString("USER_MODEL_CLASS", serStr).commit()) {
                    userModel = model;
                }
                objectOutputStream.close();
                byteArrayOutputStream.close();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
            SharedPreferences.Editor editor = preferences.edit();
            if (editor.putString("USER_MODEL_CLASS", null).commit()) {
                userModel = model;
            }
        }
    }

    /**
     * 获取对象
     * @return
     */
    public UserInfoEntity.UserBean getUserModel() {
        if (userModel == null) {
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
            String temp = preferences.getString("USER_MODEL_CLASS", null);
            if (temp == null) {
                return null;
            }
            String redStr;
            try {
                redStr = java.net.URLDecoder.decode(temp, "UTF-8");
                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(redStr.getBytes("ISO-8859-1"));
                ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
                userModel = (UserInfoEntity.UserBean) objectInputStream.readObject();
                objectInputStream.close();
                byteArrayInputStream.close();
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (StreamCorruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return userModel;
    }
}

